home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoys04.zip / EXESTRM.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-07  |  5KB  |  179 lines

  1. (***************************************************************************
  2.   ExeStream unit
  3.   Stream that makes adding resources to your .EXE a piece of cake
  4.   PJB November 13, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved. Portions copyright Borland.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   All seeks are relative to the first header byte.
  10.  
  11.   Does NOT work with DPMI programs. Seems to need an "patching" program.
  12.   Doubt TVRDEMO would find its resources if compiled to protected mode.
  13.   Don't use this to write to your EXE file, since
  14.   1) You can't insert new data, only overwrite
  15.   2) You'll probably get virus warnings
  16.  
  17.   To add your own resource types, follow the THeader format.
  18.   This should be the first 8 bytes in your resource:
  19.  
  20.      Signature : Word;    { Use $4246 }
  21.      InfoType  : Word;    { Any unused, not $4648 or $5250 }
  22.      InfoSize  : Longint  { Length in bytes of resource, not counting this }
  23.                           { header itself }
  24.  
  25. ***************************************************************************)
  26. unit ExeStrm;
  27.  
  28. {$B-}
  29.  
  30. interface
  31.  
  32.   uses
  33.     Objects;
  34.  
  35.   type
  36.     PExeScanningStream = ^TExeScanningStream;
  37.     TExeScanningStream =
  38.       object (TDosStream)
  39.         BasePos : Longint;
  40.         constructor Init(FileName:FNameStr; Mode:Word; InfoType:Word);
  41.         procedure Seek(Pos:Longint); virtual;
  42.       end;
  43.  
  44.   const
  45.     magicHelpFile     = $4648;
  46.     magicResourceFile = $5250;
  47.  
  48.  
  49. (***************************************************************************
  50. ***************************************************************************)
  51. implementation
  52.  
  53.   {$IFDEF DPMI}
  54.     {$DEFINE NewExeFormat}
  55.   {$ENDIF}
  56.   {$IFDEF WINDOWS}
  57.     {$DEFINE NewExeFormat}
  58.   {$ENDIF}
  59.  
  60.  
  61.   (*******************************************************************
  62.     Find the beginning of the EXE appendix
  63.   *******************************************************************)
  64.   constructor TExeScanningStream.Init(FileName:FNameStr; Mode:Word; InfoType:Word);
  65.     type
  66.      {$IFDEF NewExeFormat}
  67.       TExeHeader = record
  68.         eHdrSize:   Word;
  69.         eMinAbove:  Word;
  70.         eMaxAbove:  Word;
  71.         eInitSS:    Word;
  72.         eInitSP:    Word;
  73.         eCheckSum:  Word;
  74.         eInitPC:    Word;
  75.         eInitCS:    Word;
  76.         eRelocOfs:  Word;
  77.         eOvlyNum:   Word;
  78.         eRelocTab:  Word;
  79.         eSpace:     array [1..30] of Byte;
  80.         eNewHeader: Word;
  81.       end;
  82.      {$ENDIF}
  83.  
  84.       THeader =
  85.         record
  86.           Signature: Word;
  87.           case Integer of
  88.             0: (
  89.               LastCount: Word;
  90.               PageCount: Word;
  91.               ReloCount: Word);
  92.             1: (
  93.               InfoType: Word;
  94.               InfoSize: Longint);
  95.         end;
  96.  
  97.     var
  98.       Found  : Boolean;
  99.       Header : THeader;
  100.      {$IFDEF NewExeFormat}
  101.       ExeHeader: TExeHeader;
  102.      {$ENDIF}
  103.  
  104.   begin
  105.     inherited Init(FileName, Mode);
  106.  
  107.     BasePos:=0;
  108.     Found:=False;
  109.  
  110.     while (Status=stOK) and (BasePos<=GetSize-SizeOf(THeader)) do
  111.     begin
  112.       Seek(0);                                        (* Seek to BasePos *)
  113.       Read(Header, SizeOf(THeader));
  114.       case Header.Signature of
  115.  
  116.        {$IFDEF NewExeFormat}
  117.         $5A4D:
  118.           begin
  119.             Read(ExeHeader, SizeOf(TExeHeader));
  120.             BasePos:=ExeHeader.eNewHeader;
  121.           end;
  122.         $454E:  BasePos:=GetSize;
  123.         $4246:                                        (* Borland header *)
  124.           if Header.InfoType=InfoType then
  125.           begin
  126.             Found:=True;
  127.             Break;
  128.           end
  129.           else
  130.             case Header.Infotype of
  131.               $4C42: Dec(BasePos, Header.InfoSize-8); { Found BackLink }
  132.               else                       { Found TV HelpFile, Resource etc }
  133.                 Inc(BasePos, Header.InfoSize+8);
  134.                 { Dec(BasePos, SizeOf(THeader)*2); (* Windows helpfile? *)}
  135.             end;
  136.         $424E:
  137.           if Header.InfoType=$3230 then               { Found Debug Info }
  138.             Dec(BasePos, Header.InfoSize);
  139.         else
  140.           Break;
  141.  
  142.        {$ELSE}
  143.  
  144.         $5A4D: Inc(BasePos, LongMul(Header.PageCount, 512) -
  145.                  (-Header.LastCount and 511));
  146.         $4246:                                         (* Borland header *)
  147.           if Header.InfoType=InfoType then
  148.           begin
  149.             Found:=True;
  150.             Break;
  151.           end
  152.           else
  153.             Inc(BasePos, Header.InfoSize+8);
  154.         else
  155.           Break;
  156.        {$ENDIF}
  157.  
  158.       end;
  159.     end;
  160.  
  161.     if not Found then
  162.       Error(stInitError, 0);
  163.   end;
  164.  
  165.  
  166.   (*******************************************************************
  167.     Overridden seek does relative seeks
  168.   *******************************************************************)
  169.   procedure TExeScanningStream.Seek(Pos:Longint);
  170.   begin
  171.     inherited Seek(BasePos+Pos);
  172.   end;
  173.  
  174.  
  175.     (*******************************************************************
  176.     *******************************************************************)
  177.  
  178. end.
  179.